"""
Case Type   : GUC_ErrorLog
Case Name   : 修改参数debug_print_plan的值为on
Create At   : 2021/10/26
Owner       : peilinqian
Description :
    1.修改参数debug_print_plan为on
    2.修改参数log_min_messages的值为log
    3.进行简单的SQL操作，查看pg日志
Expect      :
    1.修改参数debug_print_plan为on,修改成功
    2.修改成功
    3.打印执行计划关键信息“LOG:  plan”
History     :
    modified：2022-3-29 by peilinqian;修改pg_log目录，避免其他日志影响
    modified by zhanghuan96,研发代码变更，日志级别log_min_messages需设置为小于等于
    log级别，日志才会打印解析树内容
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ErrorLog(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:start----')
        self.pri_dbuser = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.tb_name = 'tb_Guc_ErrorLog_Case0089'
        self.suc_flag = 'LOG:  plan'
        self.dir_new = os.path.join(macro.PG_LOG_PATH, 'pg_bak',
                                    'pg_errorlog_case0089')
        self.default_value = self.common.show_param('log_min_messages')

    def test_main(self):
        step_txt = '----查看参数debug_print_plan的默认值----'
        self.log.info(step_txt)
        result = self.pri_sh.execut_db_sql('show debug_print_plan;')
        self.log.info(f"debug_print_plan is {result}")
        self.para1 = result.strip().splitlines()[-2].strip()
        result = self.pri_sh.execut_db_sql('show log_directory;')
        self.log.info(f"log_directory is {result}")
        self.dir_init = result.strip().splitlines()[-2].strip()

        step_txt = '----step0:修改参数log_directory; expect:修改成功----'
        self.log.info(step_txt)
        msg = self.pri_sh.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f"log_directory='{self.dir_new}'",
                                        single=True)
        self.assertTrue(msg, '执行失败:' + step_txt)

        step_txt = '----step1:修改参数debug_print_plan的值为on; ' \
                   'expect:修改成功----'
        self.log.info(step_txt)
        msg = self.pri_sh.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        'debug_print_plan=on')
        self.assertTrue(msg, '执行失败:' + step_txt)

        step_txt = '----step2:修改参数log_min_messages的值为log;expect:修改成功---'
        self.log.info(step_txt)
        msg = self.pri_sh.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        'log_min_messages=log')
        self.assertTrue(msg, '执行失败:' + step_txt)

        step_txt = '----step3:进行简单的SQL操作，查看pg日志 expect:打印执行计划关键信息----'
        self.log.info(step_txt)
        sql = f'create table {self.tb_name} (id int);' \
            f'insert into {self.tb_name} values(1);'
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result,
                      '执行失败:' + step_txt)
        self.log.info('----查看pg_log----')
        shell_cmd = f'''ls -t {self.dir_new} | head -1'''
        file_name = os.path.join(self.dir_new,
                                 self.pri_root.sh(shell_cmd).result())
        self.log.info(file_name)
        shell_cmd = f"cat {file_name} | grep '{self.suc_flag}' -A 10"
        self.log.info(shell_cmd)
        result = self.pri_root.sh(shell_cmd).result()
        self.log.info(result)
        self.assertNotEqual('', result, '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        step_txt1 = '----恢复参数debug_print_plan; expect:修改成功----'
        self.log.info(step_txt1)
        msg1 = self.pri_sh.execute_gsguc('reload',
                                  self.constant.GSGUC_SUCCESS_MSG,
                                  f'debug_print_plan={self.para1}')

        step_txt2 = '----清理表数据; expect:清理成功----'
        self.log.info(step_txt2)
        drop_sql = f"drop table if exists {self.tb_name};"
        drop_result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(drop_result)

        step_txt3 = '----还原参数log_directory; expect:修改成功----'
        self.log.info(step_txt3)
        msg3 = self.pri_sh.execute_gsguc('reload',
                                         self.constant.GSGUC_SUCCESS_MSG,
                                         f"log_directory='{self.dir_init}'",
                                         single=True)

        step_txt4 = '----还原参数log_min_messages; expect:修改成功----'
        self.log.info(step_txt4)
        msg4 = self.pri_sh.execute_gsguc('reload',
                                         self.constant.GSGUC_SUCCESS_MSG,
                                         f"log_min_messages"
                                         f"='{self.default_value}'")

        step_txt = '----断言teardown执行成功----'
        self.log.info(step_txt)
        self.assertTrue(msg1, '执行失败:' + step_txt1)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, drop_result,
                      "执行失败" + step_txt2)
        self.assertTrue(msg3, '执行失败:' + step_txt3)
        self.assertTrue(msg4, '执行失败:' + step_txt4)

        self.log.info(f'----{os.path.basename(__file__)}:end----')
